#ifndef DA8677F0_C215_4354_9684_02355E2F536E
#define DA8677F0_C215_4354_9684_02355E2F536E
#pragma once

#include <iostream>
#include <cstdio>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <cstdlib>
#include <string>
#include <cstring>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include "log.hpp"

using namespace std;

#define MODE 0664
#define FILE_PATH "./myfifo"

enum
{
	FIFO_CREATE_ERR = 1,
	FIFO_DELETE_ERR,
	FIFO_OPEN_ERR,
	FIFO_READ_ERR,
	FIFO_WRITE_ERR
};

class Init
{
public:
	Init()
	{
		int n = mkfifo(FILE_PATH, MODE);
		if (n == -1)
		{
			perror("mkfifo:");
			exit(FIFO_CREATE_ERR);
		}
	}
	~Init()
	{
		int ret = unlink(FILE_PATH);
		if (ret == -1)
		{
			perror("unlink");
			exit(FIFO_DELETE_ERR);
		}
	}
};


Log log;

const int len = 4096;
const string path = "/home/yg/test.2025";
const int proj_id = 0x1111;

key_t Getkey()
{
	key_t k = ftok(path.c_str(),proj_id);
	if(k < 0){
		log(Fatal,"ftok error: %s",strerror(errno));
		exit(1);
	}
	log(Info,"ftok success,key:0x%x",k);
	return k;
}

int Getshmid(int flag)
{
	key_t k = Getkey();
	int shmid = shmget(k,len,flag);
	if(shmid == -1){
		log(Fatal,"shmget error:%s",strerror(errno));
		exit(2);
	}
	log(Info,"shmget success,shmid:%d",shmid);
	return shmid;
}

int createshm()
{
	return Getshmid(IPC_CREAT|IPC_EXCL|0666);
}

int Getshm()
{
	return Getshmid(IPC_CREAT);
}

#endif /* DA8677F0_C215_4354_9684_02355E2F536E */
